package arkanoid.components.ball;

import java.awt.Color;
import java.util.Random;

import arkanoid.components.Palette;
import arkanoid.scenes.Level;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.appearances.Circle;

public class Ball extends GameComponent<Level> {
	
	private int speed          = 200;
	private int diameter       = 15;
	private Color color;
	private BallState state;
	private double versorI, versorJ;
	
	public Ball() {		
		this.setAppearance(new Circle(Color.getHSBColor(0.56f, 1.0f, 0.8f), this.getDiameter()));
		this.setState(new AttachedBall());
	}
	
	public int getRatio() {
		return this.getDiameter() / 2;
	}
	
	public void changeColor(Color color) {
		this.setColor(color);
		this.setAppearance(new Circle(this.getColor(), this.getDiameter()));
	}
	
	public void loadBallInitialized(Palette palette) {
		Random random = new Random();
		
		this.setX(palette.getX() + palette.getWidth() / 2);
		this.setY(palette.getY() - this.getDiameter());
		this.setVersors(random);
	}
	
	@Override
	public void onSceneActivated() {
		Palette palette = this.getScene().getPalette();
		this.loadBallInitialized(palette);
	}
	
	public void setVersors(Random random) {
		this.setVersorI(0.5 + (random.nextDouble() * (2 - 0.5) + 1));
		this.setVersorJ(-Math.abs(0.5 + (random.nextDouble() * (2 - 0.5) + 1)));

		double module = Math.sqrt(this.getVersorI() * this.getVersorI() + this.getVersorJ() * this.getVersorJ());

		this.setVersorI(this.getVersorI() / module);
		this.setVersorJ(this.getVersorJ() / module);
	}
	
	@Override
	public void update(DeltaState deltaState) {
		super.update(deltaState);
		this.getState().update(this, deltaState);
	}

	
	// ************************************************************
	// ** ACCESSORS
	// ************************************************************
	
	public int getSpeed() {
		return speed;
	}
	
	public int getDiameter() {
		return diameter;
	}
	
	public Color getColor() {
		return color;
	}
	
	public double getVersorI() {
		return versorI;
	}
	
	public void setVersorI(double posicionI) {
		this.versorI = posicionI;
	}
	
	public double getVersorJ() {
		return versorJ;
	}
	
	public void setVersorJ(double posicionJ) {
		this.versorJ = posicionJ;
	}
	
	public void setSpeed(int speed) {
		this.speed = speed;
	}
	
	public BallState getState() {
		return state;
	}
	
	public void setState(BallState state) {
		this.state = state;
	}
	
	public void setColor(Color color) {
		this.color = color;
	}
}